package com.sky.service.impl;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;
//    @Override
//    public ShoppingCart selectBy(ShoppingCart cart) {
//        return shoppingCartMapper.selectBy(cart);
//    }

    @Override
    public void addShoppingCart(ShoppingCartDTO dto) {
        //属性copy
        ShoppingCart cart=new ShoppingCart();
        BeanUtils.copyProperties(dto,cart);
        cart.setUserId(BaseContext.getCurrentId());//设置用户id

        //查询是否有对应的套餐或者菜品订单在购物车
        ShoppingCart shoppingCart = shoppingCartMapper.selectBy(cart);
        if(shoppingCart==null){//购物车没有该菜品或者套餐

            if(cart.getDishId()!=null){//不为空说明购物车里的是菜品不是套餐
                //查询对应要添加的菜品信息
                DishVO dish = dishMapper.getDish(cart.getDishId());
                cart.setName(dish.getName());
                cart.setAmount(dish.getPrice());
                cart.setImage(dish.getImage());

            }else {//否则购物车里的是套餐
                SetmealVO setmealById = setmealMapper.getSetmealById(cart.getSetmealId());
                cart.setName(setmealById.getName());
                cart.setAmount(setmealById.getPrice());
                cart.setImage(setmealById.getImage());

            }
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(cart);

        }else{//购物车有
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.update(shoppingCart);
        }
    }


    /**
     * 购物车商品减一
     * @param dto
     */
    @Transactional
    @Override
    public void sub(ShoppingCartDTO dto) {
        //属性copy
        ShoppingCart cart=new ShoppingCart();
        BeanUtils.copyProperties(dto,cart);
        cart.setUserId(BaseContext.getCurrentId());//设置用户id

        //查对应的用户的购物车中对应的要减少的草品或者套餐
        ShoppingCart shoppingCart = shoppingCartMapper.selectBy(cart);
        if(shoppingCart.getNumber()>1){//判断数量，超过1数量减少1
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.update(shoppingCart);
            return;
        }
        //数量等于1直接删除
        shoppingCartMapper.delete(cart);

    }

    @Override
    public void clean() {
        shoppingCartMapper.clean (BaseContext.getCurrentId());
    }

    @Override
    public List<ShoppingCart> getShoppingCart() {
//        List<ShoppingCart> shoppingCart = shoppingCartMapper.getShoppingCart(BaseContext.getCurrentId());
        return shoppingCartMapper.getShoppingCart(BaseContext.getCurrentId());
    }
}
